﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Tiwo.Design
{
    public partial class SelectTypeSource : Form
    {
        public SelectTypeSource()
        {
            InitializeComponent();
        }

        public IEnumerable<ITypeSource> TypeSourceList
        {
            get
            {
                return ((IEnumerable<TypeSourceWrapper>)lbTypes.DataSource).Select(v => v.TypeSource);
            }
            set
            {
                lbTypes.DataSource = value
                    .Select(v => new TypeSourceWrapper(v))
                    .OrderBy(v => v.ToString(), StringComparer.InvariantCultureIgnoreCase)
                    .ToArray();
            }
        }

        public ITypeSource TypeSource
        {
            get
            {
                return ((TypeSourceWrapper)lbTypes.SelectedItem).TypeSource;
            }
        }

        private void lbTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSelect.Enabled = lbTypes.SelectedIndex >= 0;
        }

        private class TypeSourceWrapper
        {
            public ITypeSource TypeSource { get; private set; }

            public TypeSourceWrapper(ITypeSource typeSource)
            {
                TypeSource = typeSource;
            }

            public override string ToString()
            {
                return TypeSource.FriendlyName;
            }
        }
    }
}
